#include "CAnimatedObject.h"

USING_NS_CC;

CAnimatedObject::CAnimatedObject():
	_pAnimations(NULL),
	_pSpriteSheet(NULL),
	_delete(false),
	_pWorld(NULL),
	_pBody(NULL)
{
}

CAnimatedObject::~CAnimatedObject()
{
	_pAnimations->release();
}

/**
 * Initialize the ship, engine and shield objects.
 *
 * @param frameNamePrefix The frame prefix for the ship sprite.
 * @param pSpriteSheet This parameter is created and released by the MainLayer.
 *
 * @return true if the sprite was initialized without problems, false otherwise.
 */
bool
CAnimatedObject::init(char* frameNamePrefix, CCSpriteBatchNode* pSpriteSheet, b2World* pWorld)
{
	_pSpriteSheet = pSpriteSheet;
	_pWorld = pWorld;
	//Create game object sprite and add it to the sprite sheet.
	char frameName[512];
	memset(frameName,NULL,sizeof(frameName));
	sprintf(frameName,"%s1.png",frameNamePrefix);

	if(!CCSprite::initWithSpriteFrameName(frameName))
	{
		return false;
	}

	_pAnimations = new CCArray();

	float spriteWidth = this->getContentSize().width;
	this->setPosition(ccp(-spriteWidth, 0.0f));
	_pSpriteSheet->addChild(this);

	return true;
}

/**
 * Add a new animation to the CCAnimationCache.
 *
 * @param pAnimationProperties Animation name, frame count and delay between frames.
 */
void
CAnimatedObject::addAnimation(CAnimationProperties* pAnimationProperties)
{
	const char* animationName = pAnimationProperties->_animationName;
	CCAnimation* pAnimation = CCAnimationCache::sharedAnimationCache()->animationByName(animationName);

	if(pAnimation == NULL)
	{
		CCArray* pAnimationFrames = new CCArray();
		char framename[512];
		for(int i = 1; i <= pAnimationProperties->_frameCount; ++i)
		{
			memset(framename,NULL,sizeof(framename));
			sprintf(framename,"%s%i.png",pAnimationProperties->_animationName,i);
			CCSpriteFrame* pSpriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(framename);
			pAnimationFrames->addObject(pSpriteFrame);
		}

		pAnimation = CCAnimation::animationWithSpriteFrames(pAnimationFrames, pAnimationProperties->_delay);
		CCAnimationCache::sharedAnimationCache()->addAnimation(pAnimation, animationName);
		pAnimationFrames->removeAllObjects();
		pAnimationFrames->release();
	}

	_pAnimations->addObject(pAnimationProperties);
}

/**
 * Get rectangle surrounding the sprite.
 */
CCRect
CAnimatedObject::getRect()
{
	return CCRectMake(this->getPosition().x - this->getContentSize().width / 2,
					  this->getPosition().y - this->getContentSize().height / 2,
					  this->getContentSize().width,
					  this->getContentSize().height);
}

void
CAnimatedObject::destroy()
{
	if(_pWorld && _pBody)
		_pWorld->DestroyBody(this->_pBody);
	this->stopAllActions();
	if(_pSpriteSheet->getChildren()->containsObject(this))
	{
		_pSpriteSheet->removeChild(this, false);
	}
}

void
CAnimatedObject::collision()
{
}

b2Body* CAnimatedObject::getBody()
{
	return _pBody;
}

void
CAnimatedObject::collision(float damage)
{
}

bool
CAnimatedObject::getDelete()
{
	return _delete;
}

void
CAnimatedObject::setDelete(bool del)
{
	_delete = del;
}
